package com.zkca.xf.ui.activity.personprotect;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.JobLBean;
import com.zkca.xf.model.javabean.MesgLData;
import com.zkca.xf.model.javabean.uploadbean.Mesg_UP;
import com.zkca.xf.presenter.MesgGetPresenter;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.Md5Util;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.view.MesgGetView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MesActivity extends BaseTitleActivity implements MesgGetView {

    @BindView(R.id.xrv_task)
    XRecyclerView xrvTask;
    @BindView(R.id.wushuju)
    ImageView wushuju;
    private MesgGetPresenter mesgGetPresenter;
    private Mesg_UP mesg_up;
    int pageNum = 1;
    int pageSize = 10;

    private List<MesgLData.DataBean> dataBeanList = new ArrayList<>();
    private SingleAdapter<MesgLData.DataBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mes);
        ButterKnife.bind(this);
        mesgGetPresenter = new MesgGetPresenter(this, this);
        initMyView();
        initData(pageNum, 0);
        initAdapter();
    }

    private void initMyView() {
        setTitleText("消息管理");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
    }

    private void initAdapter() {
        adapter = new SingleAdapter<MesgLData.DataBean>(mContext, dataBeanList, R.layout.mesgitem) {
            @Override
            public void convert(ViewHolder holder, final MesgLData.DataBean dataBean, int position) {
                TextView megTitle = holder.getView(R.id.mesgtitle);
                TextView mesgtype = holder.getView(R.id.mesgtype);
                TextView mesgcome = holder.getView(R.id.mesgcome);
                TextView cometime = holder.getView(R.id.cometime);
                TextView guoqitime = holder.getView(R.id.guoqitime);
                TextView isHuifu = holder.getView(R.id.ishuifu);

                String title = dataBean.getTitle();
                String typeName = dataBean.getTypeName();
                long msgTime = dataBean.getRecord().getNotifyDate();
                String ymd = TimeUtils.getFormatDate2(msgTime);
                String laiyuan = dataBean.getName1();
                long expireDate = dataBean.getExpireDate();
                String ymd1 = TimeUtils.getFormatDate2(expireDate);
                int type1 = dataBean.getType1();
                String notifyStatus = dataBean.getRecord().getNotifyStatus();

                if (type1==1){
                    isHuifu.setText("不需要回复");
                }else if (type1==2){
                    if ("2".equals(notifyStatus)){
                        isHuifu.setText("未回复");
                    }else if ("3".equals(notifyStatus)){
                        isHuifu.setText("已回复");
                    }else if ("4".equals(notifyStatus)){
                        isHuifu.setText("已过期");
                    }
                }

                megTitle.setText(title);
                mesgtype.setText(typeName);
                mesgcome.setText(laiyuan);
                cometime.setText(ymd);
                guoqitime.setText(ymd1);
                holder.setItemViewOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(MesActivity.this,MesgXActivity.class);
                        intent.putExtra("mesgBean",dataBean);
                        startActivity(intent);
                        finish();
                    }
                });
            }
        };
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        xrvTask.setLayoutManager(linearLayoutManager);
        xrvTask.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));
        xrvTask.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xrvTask.setLoadingMoreEnabled(true);
                dataBeanList.clear();
                pageNum = 1;
                initData(pageNum, 0);
            }

            @Override
            public void onLoadMore() {
                pageNum++;
                initData(pageNum, 0);
            }
        });
        xrvTask.setAdapter(adapter);
    }

    private void initData(int pagenum, int result) {
        mesg_up = new Mesg_UP();
        mesg_up.setPageNum(pagenum);
        mesg_up.setPageSize(pageSize);
        mesg_up.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        mesg_up.setTimeStamp(TimeUtils.getTime());
        mesg_up.setSign(generateSign(mesg_up));
        mesgGetPresenter.getMesg(mesg_up);
    }


    public static String generateSign(Mesg_UP req) {
        String result = null;
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append("Tqp5$%HzzYrP#!00098yyYL-");
        sb.append("#");
        sb.append(req.getAccessId() + req.getAccessId());
        sb.append("#");
        result = sb.toString();
        for (int k = 0; k < 4; k++) {
            result = Md5Util.md5Sum(result);
        }
        return result;
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getMesg(MesgLData s) {

        xrvTask.refreshComplete();
        xrvTask.loadMoreComplete();
        if (s.getResult() == 0) {
            List<MesgLData.DataBean> data = s.getData();
            dataBeanList.addAll(data);

            if (s.getTotalpage() == pageNum) {
                xrvTask.setLoadingMoreEnabled(false);
            }
            if (pageNum == 1 && data.size() == 0) {
                wushuju.setVisibility(View.VISIBLE);
            } else {
                wushuju.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }



    }
}
